package com.mj.mafeed.activity;

import com.mj.mafeed.R;
import com.mj.mafeed.bean.MemberJSON;
import com.mj.mafeed.service.MemberService;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.widget.LoadingDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 登陆
 * @author zhaominglei
 * @date 2016-2-14
 * 
 */
public class LoginActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = LoginActivity.class.getSimpleName();
	public static final int MEMBER_LOGIN_FAIL = 0;
	public static final int MEMBER_LOGIN_SUCCESS = 1;
	private MemberService memberService = new MemberService();
	private ImageView goHome;
	private EditText accountEdt; //手机号
	private String account; //手机号
	private EditText passEdt; //密码
	private String password; //密码
	private TextView goRegisterTxt;
	private TextView goFindpwdTxt;
	private Button loginButton;
	private LoadingDialog loadingDialog;
	private Intent intent;
	private String referer; // 来自那个链接
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView)findViewById(R.id.login_gohome);
		accountEdt = (EditText)findViewById(R.id.login_account_edt);
		passEdt = (EditText)findViewById(R.id.login_pass_edt);
		goRegisterTxt = (TextView)findViewById(R.id.login_go_register);
		goFindpwdTxt = (TextView)findViewById(R.id.login_go_findpwd);
		loginButton = (Button)findViewById(R.id.login_login);
		
		goHome.setOnClickListener(this);
		goRegisterTxt.setOnClickListener(this);
		goFindpwdTxt.setOnClickListener(this);
		loginButton.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		
		referer = getIntent().getStringExtra("referer");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_gohome:
			intent = new Intent(getApplicationContext(), MainActivity.class);
			intent.putExtra("referer", "LoginActivity");
			intent.putExtra("member_login_success", MEMBER_LOGIN_FAIL);
			startActivity(intent);
			LoginActivity.this.finish();
			break;
		case R.id.login_go_register:
			intent = new Intent(getApplicationContext(), RegisterActivity.class);
			intent.putExtra("referer", "LoginActivity");
			startActivity(intent);
			LoginActivity.this.finish();
			break;
		case R.id.login_go_findpwd:
			intent = new Intent(getApplicationContext(), FindpwdActivity.class);
			intent.putExtra("referer", "LoginActivity");
			startActivity(intent);
			break;
		case R.id.login_login:
			login(v);
			break;
		default:
			break;
		}
	}
	
	/**
	 * 登录
	 */
	private void login(View v) {
		loginButton.setClickable(false);
		account = accountEdt.getText().toString();
		if (account == null || account.equals("")) {
			loginButton.setClickable(true);
			Toast.makeText(getApplicationContext(), R.string.login_account_hint, Toast.LENGTH_SHORT).show();
			return;
		}
		password = passEdt.getText().toString();
		if (password == null || password.equals("")) {
			loginButton.setClickable(true);
			Toast.makeText(getApplicationContext(), R.string.login_pass_hint, Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new LoginTask().execute("");
		} else {
			loginButton.setClickable(true);
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}	

	@Override
	public void onBackPressed() {
		intent = new Intent(getApplicationContext(), MainActivity.class);
		intent.putExtra("referer", "LoginActivity");
		intent.putExtra("member_login_success", MEMBER_LOGIN_FAIL);
		startActivity(intent);
		LoginActivity.this.finish();
	}
	
	public class LoginTask extends AsyncTask<String, String, MemberJSON> {
		@Override
		protected MemberJSON doInBackground(String... params) {
			return memberService.login(getApplicationContext(), referer, account, password);
		}
		@Override
		protected void onPostExecute(MemberJSON result) {
			if (result == null) {
				Toast.makeText(getApplicationContext(), R.string.login_error, Toast.LENGTH_SHORT).show();
				return;
			} else {
				Toast.makeText(getApplicationContext(), R.string.login_success, Toast.LENGTH_SHORT).show();
			}
			SPUtils.put(getApplicationContext(), "member", result);
			intent = new Intent(getApplicationContext(), MainActivity.class);
			intent.putExtra("referer", "LoginActivity");
			intent.putExtra("member_login_success", MEMBER_LOGIN_SUCCESS);
			startActivity(intent);
			LoginActivity.this.finish();
			loadingDialog.dismiss();
		}
	}
}
